<template>
  <div id="login">
    <el-card class="box-card">
      <h4 class="text-center">登录</h4>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="手机号" prop="ID">
          <el-input v-model="form.ID"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { asynclogin } from "../../api/index";
import { ElMessage } from "element-plus";
export default {
  data() {
    return {
      form: {
        ID: "",
        password: "",
      },
      rules: {
        ID: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            message: "手机号不合法",
            regexp: /^1[3456789]\d{9}$/,
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    onSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          asynclogin(this.form)
            .then(() => {
              ElMessage({
                message: "登录成功",
                type: "success",
              });
              this.$router.push({
                name: "Home",
              });
            })
            .catch((e) => {
              ElMessage({
                message: e,
                type: "error",
              });
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
#login {
  background-image: url(../../assets/image/2001911.jpg);
  height: 100vh;
  width: 100vw;
  background-repeat: no-repeat;
  background-size: cover;
  background-position-x: right;
  background-position-y: center;
  align-content: center; /* */
  display: flex;
}
.box-card {
  height: 260px;
  background-color: rgba(200, 220, 210, 0.6);
  transition: all 0.3s;
  margin: 0 auto;
  transform: translateY(100%);
}
.box-card:hover {
  background-color: rgba(200, 220, 210, 0.9);
}
</style>